package com.bjsdzk.collector.config;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

import com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory;
import com.alibaba.dubbo.rpc.cluster.loadbalance.RandomLoadBalance;
import com.bjsdzk.collector.utils.PublicUtils;
import io.vertx.redis.RedisClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;

import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcException;
import com.alibaba.dubbo.rpc.cluster.loadbalance.AbstractLoadBalance;

import lombok.Setter;
import org.springframework.stereotype.Component;

import javax.naming.spi.ObjectFactory;

/**
 * @author pc
 */
public class AllLoadBalance extends AbstractLoadBalance {


	@Setter
	private Environment environment;

	@Override
	protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
//		if (invokers.size() > 1) {
//            for (int i = 1; i < invokers.size(); i++) {
//                invokers.get(i).invoke(invocation);
//            }
//        }
		for (int i = 0; i < invokers.size(); i++) {
			Invoker<T> invoker = invokers.get(i);
			URL invokeUrl = invoker.getUrl();
			try {
				InetAddress addr = InetAddress.getLocalHost();
				if (StringUtils.equals(invokeUrl.getHost(), addr.getHostAddress())) {
					return invoker;
				}
			} catch (UnknownHostException e) {
				PublicUtils.printException(e);
			}
		}

		return invokers.get(0);
	}

}
